<template>
  <div style="z-index: 99 !important;">
    <div class="main-box1" style="display: flex;justify-content: space-evenly;flex-wrap: wrap;">
      <div class="pieEchart"></div>
      <div class="pie2Echart"></div>

    </div>
    <div class="main-box" style="display: flex;justify-content: space-evenly;flex-wrap: wrap;">
      <div class="lineEchart"></div>
      <div class="barEchart"></div>
    </div>
    <!-- <Air /> -->
    <el-dialog title="项目预警" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <router-link to="/system/message"><el-link type="warning" v-for="(item, index) in messageList" :key="index">{{ item
      }}</el-link></router-link>
      <span slot="footer" class="dialog-footer">

        <el-button @click="dialogVisible = false" type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectMessage } from "@/api/system/project";
import CountTo from 'vue-count-to'
import sessionStore from "@/sessionStore.js";
import { getProjectDataInfo, getAcceptDataInfo, getFundDataInfo, getProjectSEDataInf } from '@/api/system/lisdata.js'
import Air from '../components/SimpleAri.vue';
import * as echarts from 'echarts';
export default {
  data () {
    return {
      lineChart: '',
      barChart: '',
      piechart: '',
      propChart: '',
      dialogVisible: false,
      // 消息队列
      messageList: []
    };
  },
  components: {
    CountTo,
    Air
  },

  mounted () {
    if (sessionStore.getItem()) {
      this.dialogVisible = false
    } else {
      getProjectMessage().then(res => {
        if (res.rows.length != 0) {
          this.dialogVisible = true
          const date = new Date();
          const year = date.getFullYear();
          const month = ("0" + (date.getMonth() + 1)).slice(-2);
          const day = ("0" + date.getDate()).slice(-2);
          const formattedDate = `${year}-${month}-${day}`;
          this.messageList = res.rows.map(item => {
            let day = this.dateDiffInDays(formattedDate, item.endTime)
            return day > 0 ? item = item.name + '请在' + day + '天内提交！' : item = item.name + '逾期了' + Math.abs(day) + '天'
          })
        }

      })
      sessionStore.setItem('loginNumber', '1')
    }
    this.$nextTick(() => {
      this.LineinitChart()
      this.BarInitChart()
      this.pieinitChart()
      this.propInitChart()
    })
  },
  methods: {
    // 计算两个日期相差的天数
    dateDiffInDays (date1, date2) {
      const oneDay = 24 * 60 * 60 * 1000; // 毫秒数
      const diffInMs = Date.parse(date2) - Date.parse(date1); // 两个日期相差的毫秒数
      return Math.round(diffInMs / oneDay); // 转换为天数并四舍五入
    },
    handleSetLineChartData (type) {
      this.$emit('handleSetLineChartData', type)
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    // 饼图设置
    async pieinitChart () {
      this.piechart = echarts.init(document.querySelector('.pieEchart'))

      let res = await getProjectDataInfo()
      let data = res.rows.map((item) => {
        return { name: item.category, value: [item.total, item.done] };
      })


      let pieOption = {
        tooltip: {
          formatter: function (params) {
            var data = params.data;
            return data.name + ':<br>' + '项目总数: ' + data.value[0] + '<br>' + '验收: ' + data.value[1];
          }
        },
        series: [
          {
            name: '数据项',
            type: 'pie',
            radius: '50%',
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }


      this.pieSetOptions(pieOption)
    },
    // 验收比例饼图 设置
    async propInitChart () {
      this.propChart = echarts.init(document.querySelector('.pie2Echart'))

      let res = await getAcceptDataInfo()
      // let data = res.rows.map((item) => {
      //   return { name: item.category, value: [item.total, item.done] };
      // })
      let data = []
      for (let i = 0; i < 2; i++) {
        if (i == 0) {
          (res.rows.map((item) => {
            data.push({ name: '验收', value: item.done, itemStyle: { color: '#00ff9d' } });
          }))
        } else {
          data.push(res.rows.map((item) => {
            data.push({ name: '未验收', value: item.undone, itemStyle: { color: 'red' } });
          }))
        }
      }
      let propOption = {
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            var name = params.name;
            var value = params.value;
            var percentage = params.percent;
            return name + '<br/>' + value + ' (' + percentage + '%)';
          }

        },
        series: [
          {
            name: '数据项',
            type: 'pie',
            radius: '50%',
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }


      this.propSetOptions(propOption)
    },
    //折线图设置
    async LineinitChart () {
      this.lineChart = echarts.init(document.querySelector('.lineEchart'))
      let res = await getProjectSEDataInf()
      let month = []
      let startDate = []
      let endDate = []
      res.rows.map(item => {
        month.push(item.month)
        startDate.push(item.initiation)
        endDate.push(item.finish)
      })
      let projectStartOption = {
        title: {
          // text: 'Stacked Line'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['开始项目', '结束项目']
        },

        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: month
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '开始项目',
            type: 'line',
            stack: 'Total',
            data: startDate
          },
          {
            name: '结束项目',
            type: 'line',
            stack: 'Total',
            data: endDate
          },
        ]
      }
      this.lineSetOptions(projectStartOption)
    },
    // 柱状图设置
    async BarInitChart () {
      this.barChart = echarts.init(document.querySelector('.barEchart'))

      let res = await getFundDataInfo()
      let data = [['product', '总资金', '使用资金']]
      res.rows.map(item => {
        const date = new Date(item.month);
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const formattedDate = year + "年" + (month < 10 ? "0" + month : month) + "月";
        item.month = formattedDate
        data.push([item.month, item.used, item.total])
      })
      let propOption = {
        legend: {},
        tooltip: {},
        dataset: {
          source: data
        },
        xAxis: { type: 'category' },
        yAxis: {
          type: 'value',
        },
        series: [{ type: 'bar' }, { type: 'bar' }]
      }
      this.barSetOptions(propOption)
    },


    pieSetOptions (option) {
      this.piechart.setOption(option)
    },
    propSetOptions (option) {
      this.propChart.setOption(option)
    },

    barSetOptions (option) {
      this.barChart.setOption(option)
    },

    lineSetOptions (option) {
      this.lineChart.setOption(option)
    }
  }
}
</script>

<style lang="scss" scoped>
.lineEchart,
.barEchart,
.pieEchart,
.pie2Echart {
  width: 50%;
  height: 500px;
}

.panel-group {
  margin-top: 18px;
  padding: 0 5rem;

  .card-panel-col {
    margin-bottom: 32px;
  }


  .card-panel {

    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px 26px 26px 0;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
